﻿#include "Game.h"

// lib
#include <glm/gtc/matrix_transform.hpp>

// std
#include <vector>

Game *s_instance = nullptr;

Game::Game(const ConfigArgs &args)
    : Application(args.appArgs)
{
  assert(!s_instance && "Game already exists!");
  s_instance = this;

  m_input = CreateScoped<Input>();
  m_input->InitCallbacks(m_window);
  m_level = CreateRef<Level>(m_args.saveName);

  m_pluginManager.LoadPluginDir();
  LOG_INFO("Loaded {} Plugin(s)", m_pluginManager.plugins.size());
}

Game::~Game() {
  if (s_instance == this) {
    s_instance = nullptr;
  }
}

Game &Game::GetInstance()
{
  assert(s_instance && "Game does not exist!");
  return *s_instance;
}

void Game::OnInit()
{
  m_pluginManager.CallHook("OnInit");
}

void Game::OnUpdate(float deltaTime)
{
  m_level->Update(deltaTime);
  m_pluginManager.CallHook("OnUpdate");

  if (m_input->IsKeyJustReleased(GLFW_KEY_U))
  {
    m_level->ToggleGui();
  }

  m_input->NextFrame();
}

void Game::OnRender()
{
  m_level->Render();
  m_pluginManager.CallHook("OnRender");
}

void Game::OnRenderGui()
{
  m_level->RenderGui();
  m_pluginManager.CallHook("OnRenderGui");
}

void Game::OnShutdown()
{
}

void Game::SetGameMode(Mode mode)
{
  m_gameMode = mode;
}
